﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;

namespace Rsa
{
    /// <summary>
    /// Interaction logic for Configuracao.xaml
    /// </summary>
    public partial class Configuracao : UserControl
    {
        private GestorConfigXML gxml;
        private GestorTwitter twitter;
        private GestorFacebook face;

        public Configuracao(GestorConfigXML gx)
        {
            InitializeComponent();
            gxml = gx;
            this.carregaConfigs();
        }

        public void carregaConfigs()
        {
            //carrega configs do moodle
            Hashtable hash = gxml.loadMoodle();
            usernameMoodleTextBox.Text = (string)hash["username"];
            passwordMoodleTextBox.Password = (string)hash["password"];


            //carrega configs do email
            hash = gxml.loadEmail();
            emailUsernameTextBox.Text = (string)hash["username"];
            emailPasswordTextBox.Password = (string)hash["password"];
            emailImapServidorTextBox.Text = (string)hash["imapServidor"];
            emailImapPortaTextBox.Text = (string)hash["imapPorta"];
            emailImapEncryptTextBox.Text = (string)hash["imapEncrypt"];
            emailSmtpServidorTextBox.Text = (string)hash["smtpServidor"];
            emailSmtpPortaTextBox.Text = (string)hash["smtpPorta"];
            emailSmtpEncryptTextBox.Text = (string)hash["smtpEncrypt"];

            //carrega configs Geral
            hash = gxml.loadGeral();
            int encrypt = Convert.ToInt32(hash["encrypt"]);
            if (encrypt == 0)
                encryptCheckBox.IsChecked = false;
            else
                encryptCheckBox.IsChecked = true;

            //carrega configs Ementa
            hash = gxml.loadEmenta();

            cantina1TextBox.Text = (string)hash["cantina1_url"];
            cantina2TextBox.Text = (string)hash["cantina2_url"];
            cantina3TextBox.Text = (string)hash["cantina3_url"];
            cantina4TextBox.Text = (string)hash["cantina4_url"];
            cantina5TextBox.Text = (string)hash["cantina5_url"];
            diasMostrarTextBox.Text = (string)hash["dias_mostrar"];

            int campus1 = Convert.ToInt32(hash["cantina1_cb"]);
            if (campus1 == 0)
                cantina1CheckBox.IsChecked = false;
            else
                cantina1CheckBox.IsChecked = true;

            int campus2 = Convert.ToInt32(hash["cantina2_cb"]);
            if (campus2 == 0)
                cantina2CheckBox.IsChecked = false;
            else
                cantina2CheckBox.IsChecked = true;

            int campus3 = Convert.ToInt32(hash["cantina3_cb"]);
            if (campus3 == 0)
                cantina3CheckBox.IsChecked = false;
            else
                cantina3CheckBox.IsChecked = true;

            int campus4 = Convert.ToInt32(hash["cantina4_cb"]);
            if (campus4 == 0)
                cantina4CheckBox.IsChecked = false;
            else
                cantina4CheckBox.IsChecked = true;

            int campus5 = Convert.ToInt32(hash["cantina5_cb"]);
            if (campus5 == 0)
                cantina5CheckBox.IsChecked = false;
            else
                cantina5CheckBox.IsChecked = true;
            

        }

        

        private void gravarConfigGeralButton_Click(object sender, RoutedEventArgs e)
        {
            bool? myValue = encryptCheckBox.IsChecked;
            int valDrop = 0;
            if (myValue.HasValue && myValue.Value)
                valDrop = 1;
            else
                valDrop = 0;

            gxml.guardaGeral(valDrop);
           
        }

        private void gravarConfigEmentaButton_Click(object sender, RoutedEventArgs e)
        {
            int ementa1 = 0;
            int ementa2 = 0;
            int ementa3 = 0;
            int ementa4 = 0;
            int ementa5 = 0;

            bool? val = cantina1CheckBox.IsChecked;
            if (val.HasValue && val.Value)
                ementa1 = 1;

            val = cantina2CheckBox.IsChecked;
            if (val.HasValue && val.Value)
                ementa2 = 1;

            val = cantina3CheckBox.IsChecked;
            if (val.HasValue && val.Value)
                ementa3 = 1;

            val = cantina4CheckBox.IsChecked;
            if (val.HasValue && val.Value)
                ementa4 = 1;

            val = cantina5CheckBox.IsChecked;
            if (val.HasValue && val.Value)
                ementa5 = 1;


            gxml.guardaEmenta(cantina1TextBox.Text, cantina2TextBox.Text,cantina3TextBox.Text,cantina4TextBox.Text,cantina5TextBox.Text, ementa1, ementa2, ementa3, ementa4, ementa5, Convert.ToInt32(diasMostrarTextBox.Text));
            MessageBoxResult result = MessageBox.Show("Alterações Guardadas");  
            
        }

        private void gravarConfigMoodleButton_Click(object sender, RoutedEventArgs e)
        {
            gxml.guardaMoodle(usernameMoodleTextBox.Text, passwordMoodleTextBox.Password);
            MessageBoxResult result = MessageBox.Show("Alterações Guardadas");  
        }

        private void gravarConfigEmailButton_Click(object sender, RoutedEventArgs e)
        {
            gxml.guardaEmail(emailUsernameTextBox.Text, emailPasswordTextBox.Password, emailImapServidorTextBox.Text,
                emailImapPortaTextBox.Text, emailImapEncryptTextBox.Text, emailSmtpServidorTextBox.Text,
                emailSmtpPortaTextBox.Text, emailSmtpEncryptTextBox.Text);
            MessageBoxResult result = MessageBox.Show("Alterações Guardadas");  
        }

        private void buttonRegistarTwitter_Click(object sender, RoutedEventArgs e)
        {
            buttonSalvarTwitter.IsEnabled = true;
            textBoxTwitter.IsEnabled = true;

          //  twitter.AcessToken = "935512681-GutFf3R5KWIiYaeoSy2fnTzOyMKC2FGnFEE7Sbf8";
          //  twitter.AcessTokenSecret = "ommWU5mz0A8wmLTL08DHexC1PNBwqEpCYIZON5yGFTg";


            twitter = new GestorTwitter(gxml);
            twitter.Start();

        }

        private void buttonSalvarTwitter_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(textBoxTwitter.Password.ToString()))
            {
                Console.WriteLine("VOU GRAVAR");

                if (twitter.Auth(textBoxTwitter.Password) != false)
                {
                    MessageBoxResult mensagem = MessageBox.Show("Pin Correto");
                }
                else
                {
                    MessageBoxResult mensagem = MessageBox.Show("Pin Incorreto");
                }
                }
            else
            {
                //DAR FEEDBACK QUE ESTE CAMPO E OBRIGATORIO
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            GestorFacebook face = new GestorFacebook(gxml);
           
            face.start(0);
           
        }
    }
}
